package com.thd.springboottest.sso.server.service;

import com.thd.springboottest.sso.server.vo.SessionUtil;
import com.thd.springboottest.sso.vo.AuthBean;

import java.util.UUID;

/**
 * com.thd.springboottest.sso.server.service.SsoServerService
 *
 * @author: wanglei62
 * @DATE: 2021/8/22 16:18
 **/
public class SsoServerService {
    public static String login(String usr,String pwd){
        // 验证账号
        if(pwd.equalsIgnoreCase(usr +"1234")){
            AuthBean auth = SessionUtil.getByUsr(usr);
            if(null != auth){ // 已经登录过的用户
                return auth.getSsoToken();
            }

            // 未登录过的用户
            String ssoToken = UUID.randomUUID().toString();
            AuthBean authBean = new AuthBean(usr,pwd,ssoToken);
            SessionUtil.push(ssoToken,authBean);
            return ssoToken;
        }else{
            return "ssoserver : 用户名密码不合法";
        }

    }

    public static void logout(String ssoToken){
        SessionUtil.remove(ssoToken);
    }

    public static boolean check(String ssoToken){
        AuthBean authBean =  SessionUtil.authBeanList.get(ssoToken);
        return authBean != null;
    };

    public static AuthBean getAuth(String ssoToken){
        return SessionUtil.authBeanList.get(ssoToken);
    }


}
